﻿using Accord.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;

namespace MDK.Winform.UserControl.Config
{
    public class CameraParameterSet
    {
        static object sync = new object();
        public static bool SetCameraProcAmp(string MonikerString, VideoProcAmpProperty property, int value, VideoProcAmpFlags controlFlags)
        {
            bool ret = true;
            // check if source was set
            if ((MonikerString == null) || (string.IsNullOrEmpty(MonikerString)))
            {
                throw new ArgumentException("Video source is not specified.");
            }

            lock (sync)
            {
                object tempSourceObject = null;

                // create source device's object
                try
                {
                    tempSourceObject = FilterInfo.CreateFilter(MonikerString);
                }
                catch
                {
                    throw new ApplicationException("Failed creating device object for moniker.");
                }
                IAMVideoProcAmp videoProcAmp = (IAMVideoProcAmp)tempSourceObject;
                int hr = videoProcAmp.Set(property, value, controlFlags);

                ret = (hr >= 0);

                Marshal.ReleaseComObject(tempSourceObject);
            }
            return ret;
        }
    }
    /// <summary>
    /// From VideoProcAmpProperty
    /// </summary>
    public enum VideoProcAmpProperty
    {
        /// <summary>
        /// 亮度
        /// </summary>
        Brightness,
        /// <summary>
        /// 对比度
        /// </summary>
        Contrast,
        /// <summary>
        /// 色相
        /// </summary>
        Hue,
        /// <summary>
        /// 饱和度
        /// </summary>
        Saturation,
        /// <summary>
        /// 清晰度
        /// </summary>
        Sharpness,
        /// <summary>
        /// 伽马
        /// </summary>
        Gamma,
        /// <summary>
        /// 启用颜色
        /// </summary>
        ColorEnable,
        /// <summary>
        /// 白平衡
        /// </summary>
        WhiteBalance,
        /// <summary>
        /// 逆光对比
        /// </summary>
        BacklightCompensation,
        /// <summary>
        /// 增益
        /// </summary>
        Gain
    }


    /// <summary>
    /// From VideoProcAmpFlags
    /// </summary>
    [Flags]
    public enum VideoProcAmpFlags
    {
        /// <summary>
        /// 默认
        /// </summary>
        None = 0,
        /// <summary>
        /// 自动
        /// </summary>
        Auto = 0x0001,
        /// <summary>
        /// 手动
        /// </summary>
        Manual = 0x0002
    }


    [ComImport, System.Security.SuppressUnmanagedCodeSecurity,
       Guid("C6E13360-30AC-11d0-A18C-00A0C9118956"),
       InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IAMVideoProcAmp
    {
        [PreserveSig]
        int GetRange(
            [In] VideoProcAmpProperty Property,
            [Out] out int pMin,
            [Out] out int pMax,
            [Out] out int pSteppingDelta,
            [Out] out int pDefault,
            [Out] out VideoProcAmpFlags pCapsFlags
            );

        [PreserveSig]
        int Set(
            [In] VideoProcAmpProperty Property,
            [In] int lValue,
            [In] VideoProcAmpFlags Flags
            );

        [PreserveSig]
        int Get(
            [In] VideoProcAmpProperty Property,
            [Out] out int lValue,
            [Out] out VideoProcAmpFlags Flags
            );
    }
}
